package draw;

import graph.WeightedDirectedGraph;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JFrame;

import puzzel.Position;
import puzzel.PuzzleColor;
import puzzel.State;

public class Apl {
	public static void main(String[] args) {


		WeightedDirectedGraph<Position,PuzzleColor> graph = loadMaze();
		
		JFrame frame = new JFrame();
		frame.setSize(900, 900);
		frame.setVisible(true);

		
		PhysicalGraph pGraph = new PhysicalGraph(graph);
		GraphView view = new GraphView(pGraph);

		frame.setContentPane(view);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		view.repaint();
		
	}

	public static WeightedDirectedGraph<Position, PuzzleColor> loadMaze() {

		WeightedDirectedGraph<Position, PuzzleColor> graph = new WeightedDirectedGraph<Position, PuzzleColor>();

		try {
			File file = new File("graph.txt");
			FileReader fileReader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String line = bufferedReader.readLine();

			Map<Integer,Position> positions = new LinkedHashMap<Integer,Position>();

			while ((line = bufferedReader.readLine()) != null) {
				String[] data = line.split(",");
				if (data[0].equals("vertex")) {
					String[] data2 = data[1].split("-");
					PuzzleColor c = null;
					if (data2[1].equals("zwart"))
						c = PuzzleColor.zwart;
					if (data2[1].equals("rood"))
						c = PuzzleColor.rood;
					if (data2[1].equals("groen"))
						c = PuzzleColor.groen;
					if (data2[1].equals("geel"))
						c = PuzzleColor.geel;
					if (data2[1].equals("blauw"))
						c = PuzzleColor.blauw;
					Position p = new Position(Integer.parseInt(data2[0]),c);
					positions.put(Integer.parseInt(data2[0]),p);
					graph.putVertex(p);
				} else if (data[0].equals("edge")) {
					String[] data2 = data[1].split("-");
					PuzzleColor c = null;
					if (data2[1].equals("zwart"))
						c = PuzzleColor.zwart;
					if (data2[1].equals("rood"))
						c = PuzzleColor.rood;
					if (data2[1].equals("groen"))
						c = PuzzleColor.groen;
					if (data2[1].equals("geel"))
						c = PuzzleColor.geel;
					if (data2[1].equals("blauw"))
						c = PuzzleColor.blauw;
					graph.addEdge(c,positions.get(Integer.parseInt(data2[0])),positions.get(Integer.parseInt(data2[2])));
				}

			}
		} catch (IOException e) {

		}
		return graph;
	}
}
